/* vi: set sw=4 ts=4: */
/*
 * g-china-date.c
 *
 * This file is part of ________.
 *
 * Copyright (C) 2007 - yetist <yetist@gmail.com>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 * */

#include <glib/gi18n.h>
#include "gcn-table.h"

#define REFERENCE_YEAR  1201

struct _GChinaDate
{
    ChinaDate *solar;
    ChinaDate *lunar;
    ChinaDate *lunar2;
    ChinaDate *gan;
    ChinaDate *zhi;
    ChinaDate *gan2;
    ChinaDate *zhi2;
    glong       days;
    guint   lunar_year_months[NUM_OF_YEARS];
    guint   lunar_year_days[NUM_OF_YEARS];
    guint   lunar_month_days[NUM_OF_MONTHS +1];
};

static int days_in_solar_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

/* a short (long) lunar month has 29 (30) days */
static int days_in_lunar_month[2] = {29,30}; 

static	char	*gan_list[] = {
    N_("Jia3"),	N_("Yi3"),	 N_("Bing3"), N_("Ding1"), N_("Wu4"),
    N_("Ji3"),	N_("Geng1"), N_("Xin1"),  N_("Ren2"),  N_("Gui3")
};

static	char	*zhi_list[] = {
    N_("Zi3"),	N_("Chou3"),  N_("Yin2"),  N_("Mao3"),  N_("Chen2"), N_("Si4"),
    N_("Wu3"),	N_("Wei4"),	 N_("Shen1"), N_("You3"),  N_("Xu1"),   N_("Hai4")
};

static	char   *shengxiao_list[] = {
    N_("Mouse"), N_("Ox"), N_("Tiger"), N_("Rabbit"), N_("Dragon"), N_("Snake"),
    N_("Horse"), N_("Goat"), N_("Monkey"), N_("Rooster"), N_("Dog"), N_("Pig")
};


static	char	*GanGB[] = {
    "甲", "乙", "丙", "丁", "戊",
    "己", "庚", "辛", "壬", "癸"
};

static	char	*ZhiGB[] = {
    "子", "丑", "寅", "卯", "辰", "巳",
    "午", "未", "申", "酉", "戌", "亥"
};

static	char   *ShengXiaoGB[] = {
    "鼠", "牛", "虎", "兔", "龙", "蛇",
    "马", "羊", "猴", "鸡", "狗", "猪"
};

static void _china_date_calc_lunar(GChinaDate *date, GError **error);
static void _china_date_calc_solar(GChinaDate *date, GError **error);
static gint _china_date_make_lunar_month_days(GChinaDate *date, gint year);
static void _china_date_make_all_lunar_data(GChinaDate *date);
static void _china_date_days_to_lunar (GChinaDate *date, GError **error);
static void _china_date_days_to_solar(GChinaDate *date, GError **error);
static void _china_date_calc_ganzhi(GChinaDate *date);
static void _china_date_calc_bazi(GChinaDate *date);
static gint _china_date_get_bazi_lunar (GChinaDate *date);
static glong _date_calc_days_since_reference_year (ChinaDate *d, GError **error);
static void _date_calc_days_since_lunar_year (GChinaDate *date, GError **error);

GQuark g_china_date_error_quark (void)
{
    static GQuark quark = 0;

    if (G_UNLIKELY (quark == 0))
        quark = g_quark_from_static_string ("g-china-date-error-quark");

    return quark;
}

static gboolean leap (guint year)
{
    return((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0));
} 
static gint _cmp_date (gint month1, gint day1, gint month2, gint day2)
{
    if (month1!=month2) return(month1-month2);
    if (day1!=day2) return(day1-day2);
    return(0);
}

/**
 * g_china_date_new:
 *
 *
 *
 * Return value: a newly-allocated #GChinaDate
 **/
GChinaDate*     g_china_date_new                (void)
{
    GChinaDate *date = g_new0 (GChinaDate, 1);
    date->solar = g_new0 (ChinaDate, 1);
    date->lunar = g_new0 (ChinaDate, 1);
    date->lunar2 = g_new0 (ChinaDate, 1);
    date->gan   = g_new0 (ChinaDate, 1);
    date->zhi   = g_new0 (ChinaDate, 1);
    date->gan2   = g_new0 (ChinaDate, 1);
    date->zhi2   = g_new0 (ChinaDate, 1);
    _china_date_make_all_lunar_data(date);
    return date;
}

/**
 * g_china_date_set_solar_date:
 * @date: 
 * @year: 
 * @month: 
 * @day: 
 * @hour: 
 * @error: 
 *
 *
 **/
void            g_china_date_set_solar_date     (GChinaDate *date,
        GDateYear year,
        GDateMonth month,
        GDateDay day,
        GDateHour hour,
        GError **error)
{
    GError *calc_error = NULL;

    if (!(year>=BEGIN_YEAR && year< BEGIN_YEAR+NUM_OF_YEARS))
        g_set_error(error, G_CHINA_DATE_ERROR,
                G_CHINA_DATE_ERROR_YEAR,
                _("Year out of range."));
        return;

    date->solar->year = year;
    date->solar->month = month;
    date->solar->day = day;
    if (hour == 0)
        date->solar->hour = 0;
    /* 计算农历 */
    _china_date_calc_lunar(date, &calc_error);
    if (calc_error)
    {
        g_propagate_error (error, calc_error);
        return;
    }
    _china_date_calc_ganzhi(date);
    _china_date_calc_bazi(date);

}

/**
 * g_china_date_set_lunar_date:
 * @date: 
 * @year: 
 * @month: 
 * @day: 
 * @hour: 
 * @isleap: 
 * @error: 
 *
 *
 **/
void            g_china_date_set_lunar_date     (GChinaDate *date,
        GDateYear year,
        GDateMonth month, 
        GDateDay day,
        GDateHour hour,
        gboolean isleap,
        GError **error)
{
    GError *calc_error = NULL;

    if (!(year>=BEGIN_YEAR && year< BEGIN_YEAR+NUM_OF_YEARS))
        g_set_error(error, G_CHINA_DATE_ERROR,
                G_CHINA_DATE_ERROR_YEAR,
                _("Year out of range."));
        return;

    date->lunar->year = year;
    date->lunar->month = month;
    date->lunar->day = day;
    if (hour == 0)
        date->lunar->hour = 0;
    else
        date->lunar->hour = hour;
    date->lunar->isleap = isleap;
    /* 计算公历 */
    _china_date_calc_solar(date, &calc_error);
    if (calc_error)
    {
        g_propagate_error (error, calc_error);
        return;
    }
    _china_date_calc_ganzhi(date);
    _china_date_calc_bazi(date);
}

/**
 * g_china_date_get_solar_date:
 * @date: 
 * @year: 
 * @month: 
 * @day: 
 * @hour: 
 *
 *
 *
 * Return value: 
 **/
ChinaDate*     g_china_date_get_solar_date     (GChinaDate *date, gchar* year, gchar* month, gchar* day, gchar* hour)
{
    g_sprintf(year, "%u%s", date->solar->year, _("nian2"));
    g_sprintf(month, "%u%s", date->solar->month, _("yue4"));
    g_sprintf(day, "%u%s", date->solar->day, _("ri4"));
    g_sprintf(hour, "%u%s", date->solar->hour, _("shi2"));

    return date->solar;
}

/**
 * g_china_date_get_lunar_date:
 * @date: 
 * @year: 
 * @month: 
 * @day: 
 * @hour: 
 *
 *
 *
 * Return value: 
 **/
ChinaDate*     g_china_date_get_lunar_date     (GChinaDate *date, gchar* year, gchar* month, gchar* day, gchar* hour)
{

    g_sprintf(year, "%u%s", date->lunar->year, _("nian2"));
    if (date->lunar->isleap)
        g_sprintf(month, "%s%u%s", _("run4"), date->lunar->month, _("yue4"));
    else
        g_sprintf(month, "%u%s", date->lunar->month, _("yue4"));
    g_sprintf(day, "%u%s", date->lunar->day, _("ri4"));
    g_sprintf(hour, "%u%s", date->lunar->hour, _("shi2"));
    return date->lunar;
}

/**
 * g_china_date_get_shengxiao:
 * @date: 
 *
 *
 *
 * Return value: 
 **/
gchar*          g_china_date_get_shengxiao          (GChinaDate *date)
{
    gchar* shengxiao;
    shengxiao = g_strdup(shengxiao_list[date->zhi->year]);
    return shengxiao;
}


/**
 * g_china_date_get_ganzhi:
 * @date: 
 * @year: 
 * @month: 
 * @day: 
 * @hour: 
 *
 *
 *
 * Return value: 
 **/
gchar*          g_china_date_get_ganzhi (GChinaDate *date, gchar* year, gchar* month, gchar* day, gchar* hour)
{
    g_sprintf(year, "%s%s", gan_list[date->gan->year], zhi_list[date->zhi->year]);
    g_sprintf(month, "%s%s", gan_list[date->gan->month], zhi_list[date->zhi->month]);
    g_sprintf(day, "%s%s", gan_list[date->gan->day], zhi_list[date->zhi->day]);
    g_sprintf(hour, "%s%s", gan_list[date->gan->hour], zhi_list[date->zhi->hour]);

    gchar* ganzhi = g_strdup_printf("%s%s%s%s%s%s%s%s%s%s%s%s", 
	   gan_list[date->gan->year], zhi_list[date->zhi->year], _("nian2"),
	   gan_list[date->gan->month],zhi_list[date->zhi->month], _("yue4"),
	   gan_list[date->gan->day],  zhi_list[date->zhi->day], _("ri4"),
	   gan_list[date->gan->hour], zhi_list[date->zhi->hour], _("shi2"));

    return ganzhi;
}

/**
 * g_china_date_get_bazi:
 * @date: 
 * @year: 
 * @month: 
 * @day: 
 * @hour: 
 *
 *
 *
 * Return value: 
 **/
gchar*          g_china_date_get_bazi (GChinaDate *date, gchar* year, gchar* month, gchar* day, gchar* hour)
{
    g_sprintf(year, "%s%s", gan_list[date->gan2->year], zhi_list[date->zhi2->year]);
    g_sprintf(month, "%s%s", gan_list[date->gan2->month], zhi_list[date->zhi2->month]);
    g_sprintf(day, "%s%s", gan_list[date->gan2->day], zhi_list[date->zhi2->day]);
    g_sprintf(hour, "%s%s", gan_list[date->gan2->hour], zhi_list[date->zhi2->hour]);

    gchar* bazi = g_strdup_printf("%s%s%s%s%s%s%s%s%s%s%s%s\n", 
	   gan_list[date->gan2->year], zhi_list[date->zhi2->year], _("nian2"),
	   gan_list[date->gan2->month],zhi_list[date->zhi2->month], _("yue4"),
	   gan_list[date->gan2->day],  zhi_list[date->zhi2->day], _("ri4"),
	   gan_list[date->gan2->hour], zhi_list[date->zhi2->hour], _("shi2"));

    return bazi;

}

/**
 * g_china_date_free:
 * @date: 
 *
 *
 **/
void            g_china_date_free                   (GChinaDate *date)
{
    g_return_if_fail (date != NULL);

    g_free(date->solar);
    g_free(date->lunar);
    g_free(date->lunar2);
    g_free(date->gan);
    g_free(date->zhi);
    g_free(date->gan2);
    g_free(date->zhi2);
    g_free(date);

}

static void _china_date_calc_lunar(GChinaDate *date, GError **error)
{
    glong days;
    GError *calc_error = NULL;

    ChinaDate *d;
    date->days = _date_calc_days_since_reference_year(date->solar, &calc_error) ;
    if (calc_error)
    {
        g_propagate_error (error, calc_error);
        return;
    }
    date->days -=  _date_calc_days_since_reference_year(&first_solar_date, &calc_error);
    if (calc_error)
    {
        g_propagate_error (error, calc_error);
        return;
    }
    /* A lunar day begins at 11 p.m. */
    if (date->solar->hour == 23)
        date->days ++;

    _china_date_days_to_lunar(date, &calc_error);
    if (calc_error)
    {
        g_propagate_error (error, calc_error);
        return;
    }
    date->lunar->hour = date->solar->hour;
}

static void _china_date_calc_solar(GChinaDate *date, GError **error)
{
    GError *calc_error = NULL;
    _date_calc_days_since_lunar_year(date, &calc_error);
    if (calc_error)
    {
        g_propagate_error (error, calc_error);
        return;
    }
    _china_date_days_to_solar(date, &calc_error);
    if (calc_error)
    {
        g_propagate_error (error, calc_error);
        return;
    }
    date->solar->hour = date->lunar->hour;
}

//TODO
static void _china_date_calc_ganzhi(GChinaDate *date)
{
    int	year, month;
    year = date->lunar->year - first_lunar_date.year;
    month = year * 12 + date->lunar->month - 1;   /* leap months do not count */

    date->gan->year = (first_gan_date.year + year) % 10;
    date->zhi->year = (first_zhi_date.year + year) % 12;
    date->gan->month = (first_gan_date.month + month) % 10;
    date->zhi->month = (first_zhi_date.month + month) % 12;
    date->gan->day = (first_gan_date.day + date->days) % 10;
    date->zhi->day = (first_zhi_date.day + date->days) % 12;
    date->zhi->hour = ((date->lunar->hour + 1) / 2) % 12;
    date->gan->hour = (date->gan->day * 12 + date->zhi->hour) % 10;
}

static void _china_date_calc_bazi(GChinaDate *date)
{
    int	year, month;

    _china_date_get_bazi_lunar(date);

    year = date->lunar2->year - first_lunar_date.year;
    month = year * 12 + date->lunar2->month - 1;   /* leap months do not count */

    date->gan2->year = (first_gan_date.year + year) % 10;
    date->zhi2->year = (first_zhi_date.year + year) % 12;
    date->gan2->month = (first_gan_date.month + month) % 10;
    date->zhi2->month = (first_zhi_date.month + month) % 12;
    date->gan2->day = (first_gan_date.day + date->days) % 10;
    date->zhi2->day = (first_zhi_date.day + date->days) % 12;
    date->zhi2->hour = ((date->lunar2->hour + 1) / 2) % 12;
    date->gan2->hour = (date->gan2->day * 12 + date->zhi2->hour) % 10;
}

/* Compute the number of days from the Solar date BYEAR.1.1 */
/* 返回从阳历年1201.1.1日经过的天数 */
//long Solar2Day1(ChinaDate *d)
static glong _date_calc_days_since_reference_year (ChinaDate *d, GError **error)
{
    glong days, delta;
    int i;

    delta = d->year - REFERENCE_YEAR;
    if (delta<0) 
    {
        g_set_error(error, G_CHINA_DATE_ERROR,
                G_CHINA_DATE_ERROR_INTERNAL,
                _("Internal error: pick a larger constant for BYEAR."));
        return 0;
    }
    days = delta * 365 + delta / 4 - delta / 100 + delta / 400;
    for (i=1; i< d->month; i++)
        days += days_in_solar_month[i];
    if ((d->month > 2) && leap(d->year))
        days++;
    days += d->day - 1;

    if ((d->month == 2) && leap(d->year))
    {
        if (d->day > 29) 
        {
            g_set_error(error, G_CHINA_DATE_ERROR,
                    G_CHINA_DATE_ERROR_DAY,
                    _("Day out of range: \"%d\""),
                    d->day);
            return 0;
        }
    }
    else if (d->day > days_in_solar_month[d->month]) 
    {
        g_set_error(error, G_CHINA_DATE_ERROR,
                G_CHINA_DATE_ERROR_DAY,
                _("Day out of range: \"%d\""),
                d->day);
        return 0;
    }
    return days;
}
/* Compute offset days of a lunar date from the beginning of the table */
static void _date_calc_days_since_lunar_year (GChinaDate *date, GError **error)
{
    int year, i, m, leap_month;

    date->days = 0;
    year = date->lunar->year - first_lunar_date.year;
    for (i=0; i<year; i++)
        date->days += date->lunar_year_days[i];

    leap_month = _china_date_make_lunar_month_days(date, year);
    if ((date->lunar->isleap) && (leap_month!=date->lunar->month))
    {
        g_set_error(error, G_CHINA_DATE_ERROR,
                G_CHINA_DATE_ERROR_LEAP,
                _("%d is not a leap month in year %d.\n"), 
                date->lunar->month, date->lunar->year);
        return;
    }
    for (m=1; m<date->lunar->month; m++)
        date->days+= date->lunar_month_days[m];
    if (leap_month 
            && ((date->lunar->month>leap_month) 
                || (date->lunar->isleap && (date->lunar->month==leap_month))
               ))
        date->days += date->lunar_month_days[m++];
    date->days += date->lunar->day - 1;

    if (date->lunar->day > date->lunar_month_days[m]) 
    {
        g_set_error(error, G_CHINA_DATE_ERROR,
                G_CHINA_DATE_ERROR_DAY,
                _("Day out of range: \"%d\""),
                date->lunar->day);
        return;
    }
}

static void _china_date_days_to_lunar (GChinaDate *date, GError **error)
{
    int i, m, nYear, leap_month;

    glong offset = date->days;
    for (i=0; i<NUM_OF_YEARS && offset > 0; i++)
        offset -= date->lunar_year_days[i];
    if (offset <0)
        offset += date->lunar_year_days[--i];
    if (i==NUM_OF_YEARS) 
    {
        g_set_error(error, G_CHINA_DATE_ERROR,
                G_CHINA_DATE_ERROR_DAY,
                _("Year out of range. \"%d\""),
                date->solar->year);
        return;
    }
    date->lunar->year = i + first_lunar_date.year;

    leap_month = _china_date_make_lunar_month_days(date, i);
    for (m=1; m<=NUM_OF_MONTHS && offset >0; m++)
        offset -= date->lunar_month_days[m];
    if (offset <0)
        offset += date->lunar_month_days[--m];

    date->lunar->isleap = FALSE;	/* don't know leap or not yet */

    if (leap_month>0)	/* has leap month */
    {
        /* if preceeding month number is the leap month,
           this month is the actual extra leap month */
        date->lunar->isleap = (gboolean)(leap_month == (m - 1));

        /* month > leap_month is off by 1, so adjust it */
        if (m > leap_month) --m;
    }

    date->lunar->month = m;
    date->lunar->day = offset + 1;
}

static void _china_date_days_to_solar(GChinaDate *date, GError **error)
{
    GError *calc_error = NULL;
    gint	adj, i, m, days;
    glong offset;

    adj = (date->lunar->hour == 23)? -1 : 0;
    offset = date->days;
    offset += adj;
    offset -= _date_calc_days_since_reference_year(&first_lunar_date, &calc_error);
    if (calc_error)
    {
        g_propagate_error (error, calc_error);
        return;
    }
    offset += _date_calc_days_since_reference_year(&first_solar_date, &calc_error);
    if (calc_error)
    {
        g_propagate_error (error, calc_error);
        return;
    }
    /* offset is now the number of days from SolarFirstDate.year.1.1 */

    for (i=first_solar_date.year;
            (i<first_solar_date.year+NUM_OF_YEARS) && (offset > 0);	 i++) 
        offset -= 365 + leap(i);
    if (offset<0)
    {
        --i; 	/* LeapYear is a macro */
        offset += 365 + leap(i);
    }
    if (i==(first_solar_date.year + NUM_OF_YEARS)) 
    {
        g_set_error(error, G_CHINA_DATE_ERROR,
                G_CHINA_DATE_ERROR_DAY,
                _("Year out of range. \"%d\""),
                i);
        return;
    }
    date->solar->year = i;

    /* assert(offset<(365+LeapYear(i))); */
    for (m=1; m<=12; m++)
    {
        days = days_in_solar_month[m];
        if ((m==2) && leap(i))	/* leap February */
            days++;
        if (offset<days)
        {
            date->solar->month = m;
            date->solar->day = offset + 1;
            return;
        }
        offset -= days;
    }
}

static void _china_date_make_all_lunar_data(GChinaDate *date)
{
    gint year, i, leap;
    long code;

    for (year = 0; year < NUM_OF_YEARS; year++)
    {
        code = years_info[year];
        leap = code & 0xf;
        date->lunar_year_days[year] = 0;
        if (leap != 0)
        {
            i = (code >> 16) & 0x1;
            date->lunar_year_days[year] += days_in_lunar_month[i];
        }
        code >>= 4;
        for (i = 0; i < NUM_OF_MONTHS-1; i++)
        {
            date->lunar_year_days[year] += days_in_lunar_month[code&0x1];
            code >>= 1;
        }
        date->lunar_year_months[year] = 12;
        if (leap != 0) 
            date->lunar_year_months[year]++;
    }
}

/* Compute the days of each month in the given lunar year */
static gint _china_date_make_lunar_month_days(GChinaDate *date, gint year)
{
    int i, leap_month;
    long code;

    code = years_info[year];
    leap_month = code & 0xf;
    code >>= 4;
    if (leap_month == 0)
    {
        date->lunar_month_days[NUM_OF_MONTHS] = 0;
        for (i = NUM_OF_MONTHS-1; i >= 1; i--)
        {
            date->lunar_month_days[i] = days_in_lunar_month[code&0x1];
            code >>= 1;
        }
    }
    else
    {
        /* 
           There is a leap month (run4 yue4) L in this year.
           mday[1] contains the number of days in the 1-st month;
           mday[L] contains the number of days in the L-th month;
           mday[L+1] contains the number of days in the L-th leap month;
           mday[L+2] contains the number of days in the L+1 month, etc.

           cf. years_info[]: info about the leap month is encoded differently.
           */
        i = (years_info[year] >> 16) & 0x1;
        date->lunar_month_days[leap_month+1] = days_in_lunar_month[i];
        for (i = NUM_OF_MONTHS; i >= 1; i--)
        {
            if (i == leap_month+1) 
                i--;
            date->lunar_month_days[i] = days_in_lunar_month[code&0x1];
            code >>= 1;
        }
    }
    return leap_month;
}

/* Compare two dates and return <,=,> 0 if the 1st is <,=,> the 2nd */
static gint _china_date_get_bazi_lunar (GChinaDate *date)
{
    int m, flag;

    if (date->solar->month==1)
    {
        flag = _cmp_date(date->solar->month, date->solar->day,
                1, fest[date->solar->year - first_solar_date.year - 1][11]);
        if (flag<0) 
            date->lunar2->month = 11;
        else if (flag>0) 
            date->lunar2->month = 12;
        date->lunar2->year = date->solar->year - 1;
        return(flag==0);
    }
    for (m=2; m<=12; m++)
    {
        flag = _cmp_date(date->solar->month, date->solar->day,
                m, fest[date->solar->year - first_solar_date.year][m-2]);
        if (flag==0) m++;
        if (flag<=0) break;
    }
    date->lunar2->month = (m-2) % 12;
    date->lunar2->year = date->solar->year;
    if ((date->lunar2->month)==0)
    {
        date->lunar2->year = date->solar->year - 1;
        date->lunar2->month = 12;
    }
    return(flag==0);
}

gchar* get_jieri(JieRi type, int month, int day, gchar** jieri)
{
    int i;
    if (type == JIERI_SOLAR)
    {
        for (i=0; i < G_N_ELEMENTS(solar_jieri); i++)
        {
            gchar** b;
            b = g_strsplit(solar_jieri[i], " ", 2);
            gchar* str_day = g_strdup_printf("%02d%02d", month, day);
            if (g_ascii_strcasecmp(b[0], str_day) == 0)
            {
                *jieri = g_strdup(b[1]);
                g_strfreev(b);
                return (*jieri);
            }
            g_strfreev(b);
        }
    }
    else if (type == JIERI_LUNAR)
    {
        for (i=0; i < G_N_ELEMENTS(lunar_jieri); i++)
        {
            gchar** b;
            b = g_strsplit(lunar_jieri[i], " ", 2);
            gchar* str_day = g_strdup_printf("%02d%02d", month, day);
            if (g_ascii_strcasecmp(b[0], str_day) == 0)
            {
                *jieri = g_strdup(b[1]);
                g_strfreev(b);
                return (*jieri);
            }
            g_strfreev(b);
        }
    }
    return NULL;

}

gchar* get_week_jieri(int month, int weekth, int weekday, gchar** jieri)
{
    int i;
    for (i=0; i < G_N_ELEMENTS(week_jieri); i++)
    {
        gchar** b;
        b = g_strsplit(week_jieri[i], " ", 2);
        gchar* str_day = g_strdup_printf("%02d%01d%01d", month, weekth, weekday);
        if (g_ascii_strcasecmp(b[0], str_day) == 0)
        {
            *jieri = g_strdup(b[1]);
            g_strfreev(b);
            return (*jieri);
        }
        g_strfreev(b);
    }
    return NULL;

}

